package com.hzw.net.page.status

import android.content.Context
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import com.hzw.core.view.assignAndGetGeneratedId
import com.hzw.core.view.gone
import com.hzw.core.view.visible

class PageStatusLayout(context: Context) : ConstraintLayout(context), IPageStatusEvent {

    private val viewMap = mutableMapOf<PageStatus, View>()

    private lateinit var manager: PageStatusManager

    fun setPageStatusManager(manager: PageStatusManager) {
        this.manager = manager
        addAllLayoutToRootLayout()
    }

    /**
     * 添加布局到根ConstraintLayout上
     */
    private fun addAllLayoutToRootLayout() {
        if (manager.titleView != null) {
            // 添加 title
            val titleView = manager.titleView!!
            titleView.id = assignAndGetGeneratedId()
            val titleParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
            addView(titleView, titleParams)
        }

        // 添加内容界面
        viewMap[PageStatus.CONTENT] = manager.contentView
        addViewToLayout(manager.contentView)

        // 添加空界面，这里添加的是ViewStub， 用的时候才inflate
        addViewToLayout(manager.emptyPageStatus.viewStub)

        // 添加错误界面，这里添加的是ViewStub， 用的时候才inflate
        addViewToLayout(manager.errorPageStatus.viewStub)

        // 添加Loading界面
        addViewToLayout(manager.loadingPageStatus.viewStub)
    }

    private fun ConstraintLayout.addViewToLayout(view: View) {
        addView(view, createLayoutParams())
    }

    private fun createLayoutParams(): LayoutParams {
        val params = LayoutParams(LayoutParams.MATCH_PARENT, 0)
        if (manager.titleView != null) {
            params.topToBottom = manager.titleView!!.id
        } else {
            params.topToTop = LayoutParams.PARENT_ID
        }
        params.bottomToBottom = LayoutParams.PARENT_ID
        return params
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        viewMap.forEach {
            val pageStatus = it.value
            if (pageStatus is IPageStatus) {
                pageStatus.release()
            }
        }
        viewMap.clear()
    }

    /**
     * 是否为加载中。。。
     */
    override fun isLoading(): Boolean {
        val view = viewMap[PageStatus.LOADING]
        if (view != null) {
            return view.visibility == View.VISIBLE
        }
        return false
    }

    /**
     * 显示loading
     */
    override fun showLoading(isHideOther: Boolean) {
        var view = viewMap[PageStatus.LOADING]
        if (view == null) {
            inflateLayout(PageStatus.LOADING)
            view = viewMap[PageStatus.LOADING]
        }
        if (view != null) {
            if (isHideOther) {
                showOrHideViewByState(PageStatus.LOADING)
            } else {
                if (view.visibility == View.GONE) {
                    view.visibility = View.VISIBLE
                }
            }
            manager.loadingPageStatus.startLoading()
        }
    }

    /**
     * 隐藏Loading界面
     */
    override fun hideLoading() {
        val view = viewMap[PageStatus.LOADING]
        view?.gone()
        manager.loadingPageStatus.endLoading()
    }

    /**
     * 加载 网络错误，错误、空三种情况下的界面
     */
    private fun inflateLayout(status: PageStatus): Boolean {
        if (viewMap[status] != null) {
            return true
        }

        return when (status) {
            PageStatus.LOADING -> {
                val view = manager.loadingPageStatus.inflate()
                viewMap[PageStatus.LOADING] = view
                true
            }

            PageStatus.ERROR -> {
                val view = manager.errorPageStatus.inflate()
                viewMap[PageStatus.ERROR] = view
                manager.errorPageStatus.initView()
                true
            }
            PageStatus.EMPTY -> {
                val view = manager.emptyPageStatus.inflate()
                viewMap[PageStatus.EMPTY] = view
                manager.emptyPageStatus.initView()
                true
            }
            else -> false
        }
    }

    /**
     * 显示指定状态的View，隐藏其他状态
     */
    private fun showOrHideViewByState(status: PageStatus) {
        viewMap.forEach { item ->
            if (item.key == status) {
                item.value.visible()
            } else {
                item.value.gone()
            }
        }
    }

    /**
     * 显示内容
     */
    override fun showContent() {
        showOrHideViewByState(PageStatus.CONTENT)
    }

    /**
     * 显示空数据，修改显示内容
     */
    override fun showEmpty(imageResId: Int, text: String, retryText: String?) {
        if (inflateLayout(PageStatus.EMPTY)) {
            showOrHideViewByState(PageStatus.EMPTY)
            manager.emptyPageStatus.changeView(imageResId, text, retryText)
        }
    }

    /**
     * 显示错误界面，修改显示内容
     */
    override fun showError(imageResId: Int, text: String, retryText: String?) {
        if (inflateLayout(PageStatus.ERROR)) {
            showOrHideViewByState(PageStatus.ERROR)
            manager.errorPageStatus.changeView(imageResId, text, retryText)
        }
    }


}